import os
from pathlib import Path

from dotenv import load_dotenv
from pydantic_settings import BaseSettings, SettingsConfigDict

load_dotenv()


class Settings(BaseSettings):
    # 版本信息
    PROJECT_NAME: str = "TopoMatAPI"
    DESCRIPTION: str = "拓扑材料分析API服务，提供完整的材料拓扑性质分析工具链。"
    VERSION: str = "1.0.0"
    API_V1_STR: str = ""
    ROOT_PATH: str = ""

    # 脚本和数据目录
    SCRIPT_DIR: Path = Path("scripts")
    DATA_DIR: Path = Path("data")
    CACHE_DIR: Path = Path("cache")

    # CORS设置
    CORS_ORIGINS: str = "*"  # 使用字符串，后续会转换为列表

    model_config = SettingsConfigDict(env_file=Path.cwd() / ".env")


settings = Settings()

# 从配置获取路径并转换为Path对象
SCRIPT_DIR = settings.SCRIPT_DIR.resolve()
BILBAO_DATA = settings.DATA_DIR.resolve() / "BilBaoData"
CACHE_DIR = settings.CACHE_DIR.resolve()
DEFAULT_CACHE_DIR = CACHE_DIR / "default"
DEFAULT_CACHE_DIR.mkdir(exist_ok=True, parents=True)

if "POS2ABRDATA" not in os.environ:
    os.environ["POS2ABRDATA"] = (settings.DATA_DIR / "src_pos2aBR").resolve().as_posix()
